(add-hook 'dired-load-hook
	  (lambda ()
            (require 'dired-details)
            (setq dired-details-hidden-string "")
            (dired-details-install)
            (require 'dired-x)
	    (require 'dired-isearch)
            (setq dired-omit-files "^#\\|^\\..*") ; omit all hidden file which starts with `.'
	    (define-key dired-mode-map (kbd "C-s") 'dired-isearch-forward)
	    (define-key dired-mode-map (kbd "C-r") 'dired-isearch-backward)
	    (define-key dired-mode-map (kbd "M-C-s") 'dired-isearch-forward-regexp)
	    (define-key dired-mode-map (kbd "M-C-r") 'dired-isearch-backward-regexp)

	    (setq dired-guess-shell-alist-user nil)
            (kid-dired-define-assoc-group
             ("rm" "rmvb" "RM" "RMVB" "avi" "mpg" "mpeg")
             ("mplayer * &")
             video)
            (kid-dired-define-assoc-group
             ("pdf" "PDF")
             ("acroread * &" "xpdf * &")
             pdf-document)
            (kid-dired-define-assoc-group
             ("png" "jpg" "jpeg" "gif")
             ("xloadimage * &" "gqview * &")
             image)
            (kid-dired-define-assoc-group
             ("chm" "CHM")
             ("xchm * &")
             chm-document)
            (kid-dired-define-assoc-group
             ("html" "HTML" "htm" "HTML")
             ("firefox * &"))))
(add-hook 'dired-mode-hook (lambda () (setq dired-omit-mode 1)))

(defadvice dired-run-shell-command (around kid-dired-run-shell-command (command))
  "run a shell command COMMAND .
If the COMMAND ends with `&' then run it in background and *discard* the
output, otherwise simply let the original `dired-run-shell-command' run it."
  (if (string-match "&[[:blank:]]*$" command)
      (let ((proc (start-process "kid-shell" nil shell-file-name
                                 shell-command-switch
                                 (substring command 0 (match-beginning 0)))))
        (set-process-sentinel proc 'shell-command-sentinel))
    ad-do-it))
(ad-activate 'dired-run-shell-command)

(defmacro kid-dired-define-assoc-group (patterns actions &optional name)
  "define an assoc entry to help dired guess the shell command.
PATTERN is a list of regexps used to match the filename.
NAME is a list of string or expression which eval to a string
to denote what shell command to execute. Optional NAME is
the name of this group , just for documentation purpose."
  (let ((item (gensym)))
    `(dolist (,item ',patterns)
       (setq dired-guess-shell-alist-user
             (cons (list ,item ,@actions) dired-guess-shell-alist-user)))))

(defadvice dired-advertised-find-file (around dired-subst-directory activate)
  "Replace current buffer if file is a directory."
  (interactive)
  (let ((orig (current-buffer))
        (filename (dired-get-filename)))
    ad-do-it
    (when (and (file-directory-p filename)
               (not (eq (current-buffer) orig)))
      (kill-buffer orig))))

(eval-after-load "dired"
  ;; don't remove `other-window', the caller expects it to be there
  '(defun dired-up-directory (&optional other-window)
     "Run Dired on parent directory of current directory."
     (interactive "P")
     (let* ((dir (dired-current-directory))
     	    (orig (current-buffer))
     	    (up (file-name-directory (directory-file-name dir))))
       (or (dired-goto-file (directory-file-name dir))
     	   ;; Only try dired-goto-subdir if buffer has more than one dir.
     	   (and (cdr dired-subdir-alist)
     		(dired-goto-subdir up))
     	   (progn
     	     (kill-buffer orig)
     	     (dired up)
     	     (dired-goto-file dir))))))
(provide 'file-man)
